﻿/*
 * Class  :  Encode
 * Author : Chung Thành Phước 
 * Create Date: 18/12/2014
 * Update date: 18-12-2014 by Chung Thành Phước 
 * Reference: System.Security.Cryptography
 */

using System;
using System.Text;
using System.Security.Cryptography;

namespace Encode
{
    public class Encode
    {
        #region[GetMD5]
        public static string GetMD5(string str)
        {
            var md5 = new MD5CryptoServiceProvider();

            byte[] bHash = md5.ComputeHash(Encoding.UTF8.GetBytes(str));

            var sbHash = new StringBuilder();

            foreach (var b in bHash)
            {

                sbHash.Append(String.Format("{0:x2}", b));

            }

            return sbHash.ToString();

        }
        //-------------------------------------
        public static string EncodePassword(string originalPassword)
        {
            //Declarations
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;

            //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
            md5 = new MD5CryptoServiceProvider();
            originalBytes = Encoding.Default.GetBytes(originalPassword);
            encodedBytes = md5.ComputeHash(originalBytes);

            //Convert encoded bytes back to a 'readable' string
            return BitConverter.ToString(encodedBytes);
        }
        #endregion
    }
}